package com.example.rydwlib.fragment


import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.baselib.fragment.BaseFragment

import com.example.rydwlib.R
import com.example.rydwlib.RydwViewModel
import com.example.rydwlib.adapter.*
import kotlinx.android.synthetic.main.fragment_ry_main_alarm.*
import kotlinx.android.synthetic.main.fragment_rydw_alarm.*
import java.lang.Exception

/**
 * A simple [Fragment] subclass.
 */
class RyMainAlarmFragment : BaseFragment<RydwViewModel>() {
    private val overTimeAdapter: RyOverTimeAdapter by lazy { RyOverTimeAdapter(R.layout.ry_overtime_item) }
    private val overPersonAdapter: RyOverPersonAdapter by lazy { RyOverPersonAdapter(R.layout.ry_overperson_item) }
    private val personCallAdapter: RyPersonCallAdapter by lazy { RyPersonCallAdapter(R.layout.ry_personcall_item) }
    private val specialAlarmAdapter: RySpecialAlarmAdapter by lazy { RySpecialAlarmAdapter(R.layout.ry_specialalarm_item) }
    private val sysAlarmAdapter: RySysAlarmAdapter by lazy { RySysAlarmAdapter(R.layout.ry_sysalarm_item) }
    override fun providerVMClass(): Class<RydwViewModel>? = RydwViewModel::class.java

    override fun contentLayout(): Int = R.layout.fragment_ry_main_alarm

    override fun initView() {
        ry_main_alarm_srl.setOnRefreshListener {
            viewModel.isLoad = false
            viewModel.loadRyAlarmData()
        }
        ry_main_alarm_rcy.layoutManager =
            LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
    }

    override fun initData() {
        val emptyView = LayoutInflater.from(context).inflate(R.layout.empty_item, null, false)
        val bundle = arguments!!
        when (bundle.getInt("type")) {
            1 -> {
                overTimeAdapter.emptyView=emptyView
                ry_main_alarm_rcy.adapter=overTimeAdapter
                ry_main_alarm_type.text="人员超时报警"
            }
            2 -> {
                overPersonAdapter.emptyView=emptyView
                ry_main_alarm_rcy.adapter=overPersonAdapter
                ry_main_alarm_type.text="人员超员报警"
            }
            3 -> {
                personCallAdapter.emptyView=emptyView
                ry_main_alarm_rcy.adapter=personCallAdapter
                ry_main_alarm_type.text="人员呼救"
            }
            4 -> {
                specialAlarmAdapter.emptyView=emptyView
                ry_main_alarm_rcy.adapter=specialAlarmAdapter
                ry_main_alarm_type.text="特种人员报警"
            }
            5 -> {
                sysAlarmAdapter.emptyView=emptyView
                ry_main_alarm_rcy.adapter=sysAlarmAdapter
                ry_main_alarm_type.text="系统异常报警"
            }
        }

        viewModel.getAlarmData().observe(this, Observer {
            when (bundle.getInt("type")) {
                1 ->  overTimeAdapter.setNewData(it.overtimealarm)
                2 ->  overPersonAdapter.setNewData(it.overpersonalarm)
                3 ->   personCallAdapter.setNewData(it.personcallalarm)
                4 ->   specialAlarmAdapter.setNewData(it.specialpersonabnorma)
                5 ->  sysAlarmAdapter.setNewData(it.sysalarm)

            }
            ry_main_alarm_empty.visibility=View.INVISIBLE
        })
        if (!viewModel.getUnitInfo().isNullOrEmpty()){
            ry_main_alarm_nickname.text=viewModel.getUnitInfo()["unitname"]
        }
    }

    override fun requestFinally(it: Int?) {
        super.requestFinally(it)
        ry_main_alarm_srl.isRefreshing = false
    }
    override fun requestError(it: Exception?) {
        ry_main_alarm_empty.visibility=View.VISIBLE
        if (it?.message=="暂无数据"){
            ry_main_alarm_empty.findViewById<TextView>(R.id.err_txt).text=it?.message
            ry_main_alarm_empty.findViewById<TextView>(R.id.err_msg).visibility=View.INVISIBLE
//            ry_main_alarm_empty.findViewById<ImageView>(R.id.err_img).visibility=View.INVISIBLE
        }else{
            super.requestError(it)
            ry_main_alarm_empty.findViewById<TextView>(R.id.err_msg).text=it?.message
        }
        ry_main_alarm_empty.findViewById<Button>(R.id.err_rest_btn).setOnClickListener {  viewModel.loadRyAlarmData() }
    }
}
